package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class UserContentDisplayServlet extends HttpServlet {
    private boolean DEBUG = false;
    private EJBDatabaseQuery ejbdb;
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
	doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
	try{

	    response.setContentType("text/html");
	    PrintWriter out      = response.getWriter();
	    HttpSession session  = request.getSession(true);
	    //String call_path     = request.getServletPath();
	    String call_path     = request.getContextPath()+request.getServletPath();

	    String user_ip       = request.getRemoteAddr();
	    String action        = request.getParameter("aid");
      
	    String mid = (String)request.getAttribute("mid");
	    
	    if(mid == null) {
		System.out.println("UserContentDisplayServlet:Error:101");
		return;
	    }

	    String pid =  request.getParameter("pid");

	    if(pid == null || !MyStringCheck.isNumber(pid)) {
		pid = "1";
	    }

	    String table_name    = request.getParameter("table_name");
	    String sub_dir       = request.getParameter("sub_dir");
	    String max_rows      = request.getParameter("max_rows");

	    if(max_rows == null) {
		max_rows = "50";
	    }

	    if(table_name == null || sub_dir == null || max_rows == null) {
		System.out.println("UserContentDisplayServlet:Error:102");
		return;
	    }

	    String user_name  = null;
	    int user_age      = 0;
	    int user_level    = 0;
	    String user_fname   = null;
	    String user_since   = null;
	    String user_edu     = null;
	    String user_zip     = null;
	    String user_sex     = null;

	    Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	    if(user_info == null) {
		System.out.println("UserContentDisplayServlet:Error:104");
		return;
	    }

	    user_name   = (String)user_info.get("user_name");
	    user_age    = Integer.parseInt((String)user_info.get("user_age"));
	    user_level  = Integer.parseInt((String)user_info.get("user_level"));
	    user_fname  = (String)user_info.get("user_fname");
	    user_since  = (String)user_info.get("user_since");
	    user_edu    = (String)user_info.get("user_edu");
	    user_zip    = (String)user_info.get("user_zip");
	    user_sex    = (String)user_info.get("user_sex");

	    //////////////////////////////////////////////////////////////////////////////
	    // access control
	    //////////////////////////////////////////////////////////////////////////////

	    Hashtable achs = (Hashtable)request.getAttribute("achs");
	    if(achs == null) {
		return;
	    }

	    int read_level     = Integer.parseInt((String)achs.get("read_level"));
	    int read_age       = Integer.parseInt((String)achs.get("read_age"));
	    int post_level     = Integer.parseInt((String)achs.get("post_level"));
	    int post_age       = Integer.parseInt((String)achs.get("post_age"));

	    boolean manager  = (Boolean.valueOf((String)achs.get("show_manager"))).booleanValue();
	    boolean admin    = (Boolean.valueOf((String)achs.get("show_admin"))).booleanValue();

	    //////////////////////////////////////////////////////////////////////////////
	    // Content display Section
	    //////////////////////////////////////////////////////////////////////////////

	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String query = null;
	    UserBean ubean = new UserBean();
	    out.println(ubean.getUserSubdirInfo(sub_dir, user_name));

	    UserContentDisplayBean cbean = new UserContentDisplayBean();

	    //////////////////////////////////////////////////////////////////////////////	
	    // preview (0) list 
	    //////////////////////////////////////////////////////////////////////////////

	    if(action == null || action.equals("0")) {
		int row_counter = ejbdb.getRowCounter(table_name);
		if(row_counter == -1) {
		    if(admin) {
			out.println(cbean.adminAddNewForum(request, sub_dir, table_name, mid, "0"));
		    }
		    return;
		}
		out.println(MyHtml.tableWraper(1, "center", cbean.getAddAndSearchBar(call_path, mid, admin)));
		out.println(MyHtml.pageCounter(row_counter, max_rows, pid, call_path+"?mid="+mid));
		out.println(cbean.listContent(request, max_rows, user_name, table_name, mid, pid, admin, "1"));
		return;
	    }

	    /////////////////////////////////////////////////////////////////////////////
	    // preview (0) search list 
	    /////////////////////////////////////////////////////////////////////////////

	    if(action.equals("0-1")) {
		String search = request.getParameter("sq");
		String field[]={"FIELD5","FIELD11"};
		out.println(cbean.listSearchResults(mid, search, table_name, user_name, admin, pid, max_rows, field, "1"));
		return;
	    }

	    /////////////////////////////////////////////////////////////////////////
	    // detail review (1)
	    /////////////////////////////////////////////////////////////////////////
	    
	    if(action.equals("1")) {
		String f1  = request.getParameter("f1");
		if(f1==null || f1.length()==0) {
		    return;
		}

		String jid = request.getParameter("jid");
		boolean print = false;
		if(jid != null && jid.equals("1")) {
		    print = true;
		}

		StringBuffer sbf = new StringBuffer("");
		StringBuffer add_form_bf = null;
		String submit_id = System.currentTimeMillis()+"";
		session.setAttribute("submit_id",submit_id);
		add_form_bf = cbean.addNewPost(request, response, user_name, admin);
		sbf.append(cbean.displayDetail(request, call_path, mid, table_name, f1, pid, 
					       manager, admin, user_name, add_form_bf, print));
		out.println(sbf);
		return;
	    }	

	    //////////////////////////////////////////////////////////////////////
	    // Add (2)  (new and reply)
	    //////////////////////////////////////////////////////////////////////
	   
	    if(action.equals("2")) {
		String submit_id = System.currentTimeMillis()+"";
		session.setAttribute("submit_id",submit_id);
		out.println(cbean.addNewPost(request, response, user_name, admin));		
		return;
	    }	

	    //===================================================================
	    // User Edit
	    //===================================================================

	    if(action.equals("3-1")) {
		out.println(cbean.singlePageUserEdit(request, response, mid, table_name, user_name, "0", pid));
		return;
	    }

	    //===================================================================
	    // User delete
	    //===================================================================
	    if(action.equals("3-2")) {
		String f1 = request.getParameter("f1");
		query = "UPDATE "+table_name+" SET FIELD17='-1' WHERE FIELD7='"+user_name+"' AND FIELD1="+f1+";";
		ejbdb.update(query);
		try {
		    RequestDispatcher rd = request.getRequestDispatcher(request.getServletPath()+"?mid="+mid+"&aid=0&pid="+pid); 
		    rd.forward(request,response);
		}
		catch (Exception e) {
		    System.out.println("UserContentDisplayServlet:Error:3-3:"+e);
		}
		return;
	    }

	    //===================================================================
	    // admin Edit
	    //===================================================================

	    if(action.equals("4-1")) {
		out.println(cbean.singlePageAdminEdit(request, response, mid, table_name, "0", pid));
		return;
	    }

	    ////////////////////////////////////////////////////////////////////////////
	    // admin (4) [delete]
	    ////////////////////////////////////////////////////////////////////////////

	    if(action.equals("4-2")) {
		String f1 = request.getParameter("f1");
		query = "DELETE FROM "+table_name+" WHERE FIELD1="+f1+";";
		ejbdb.delete(query);
		try {
		    RequestDispatcher rd = request.getRequestDispatcher(request.getServletPath()+"?mid="+mid+"&aid=0&pid="+pid); 
		    rd.forward(request,response);
		}
		catch (Exception e) {
		    System.out.println("UserContentDisplayServlet:Error:4-2:"+e);
		}
		return;
	    }
	}
	catch(IOException e) {
	    System.out.println("UserContentDisplayServlet:Error:IOException:"+e);
	    return;
	}
	catch(Exception e) {
	    System.out.println("UserContentDisplayServlet:Error:Exception:"+e);
	    return;
	}
    }
}






